package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.R.layout;
import com.cn.xact.ty_treasured_book.R.menu;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeCreditHistoryActivity extends Activity {

    String[] mDate;
    String[] mDetail;

    private ListView mListView;
    private CreditHistoryListViewAdapter mCreditHistoryListViewAdapter;
    private List<Map<String, Object>> mListItems;
    private NetworkImpl mHomeExchangeHistoryNetwork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_credit_history);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_credit_history);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomeCreditHistoryActivity.this.finish();
            }

        });

        mHomeExchangeHistoryNetwork = new NetworkImpl(this, new MyNetworkCallBack());
        mHomeExchangeHistoryNetwork.getExchangeHistoryListAsync(123);

        mListView = (ListView)findViewById(R.id.home_credit_history_list_view);
        mListItems = getListItems();
        mCreditHistoryListViewAdapter = new CreditHistoryListViewAdapter(this, mListItems);
        mListView.setAdapter(mCreditHistoryListViewAdapter);
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        if (mDate != null) {
            for(int i = 0; i < mDate.length; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("date", mDate[i]);
                map.put("info", mDetail[i]);
                innnerListItems.add(map);
            }
        }
        return innnerListItems;
    }

    private class CreditHistoryListViewAdapter extends BaseAdapter {

        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public TextView date;
            public TextView info;
     }

        public CreditHistoryListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.activity_home_credit_history_list_item, null);

                listItemView.date = (TextView)convertView.findViewById(R.id.home_credit_history_list_item_date);
                listItemView.info = (TextView)convertView.findViewById(R.id.home_credit_history_list_item_detail);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            listItemView.date.setText((String)listItems.get(position).get("date"));
            listItemView.info.setText((String)listItems.get(position).get("info"));

            return convertView;
        }

    }

    class MyNetworkCallBack extends NetworkCallBack {
        @SuppressWarnings("unchecked")
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeCreditHistoryActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_EXCHANGE_HISTORY:
                mListItems = (List<Map<String, Object>>)attach1;
                mCreditHistoryListViewAdapter = new CreditHistoryListViewAdapter(HomeCreditHistoryActivity.this, mListItems);
                mListView.setAdapter(mCreditHistoryListViewAdapter);
                break;
            }
        }
    }

}
